import {
  login,
  logout,
  getInfo
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

//引入message-element
import { Message } from 'element-ui'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '' || localStorage.username,
    avatar: '',
    auth: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_AUTH: (state, value) => {
    state.auth = value
  }
}

const auths = [{
  title: '银行卡通道',
  name: 'BankCardAsile',
  auth: [{
    title: '通道管理',
    name: 'GBO_card',
  }]
}, 
{
  title: '统计报表',
  name: 'BankCardAsiles',
  auth: [{
    title: '收入统计',
    name: 'incomeStatistics',
  }, {
    title: '支出统计',
    name: 'ipOrderPay',
  }, {
    title: '财务统计',
    name: 'payStatistics',
  }]
}, 
{
  title: '管理员管理',
  name: 'systems',
  auth: [{
    title: '管理员管理',
    name: 'Role',
  }, {
    title: '权限管理',
    name: 'AuthManage',
  }, {
    title: '角色配置',
    name: 'AuthConfig',
  }]
}, 
{
  title: '系统管理',
  name: 'systemsManage',
  auth: [{
    title: '发布教程',
    name: 'course',
  }, {
    title: '下载管理',
    name: 'download',
  }, {
    title: '公告管理',
    name: 'notice',
  }]
}
]

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password
      }).then(response => {
        if (response.status == 1) {
          Message({
            type: "error",
            message: response.msg,
          })
        } else if (response.status == 0) {
          Message({
            type: 'success',
            message: response.msg
          })
        }
        const { data , auth} = response
        commit('SET_TOKEN', data.sid)
        commit('SET_NAME', username)
        localStorage.auth = JSON.stringify(auth)
        localStorage.username = username
        localStorage.sid = data.sid
        setToken(data.sid)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const {
          data
        } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const {
          name,
          avatar
        } = data
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
